#include "shapeDynamicOgre.h"

//standard library
#include <string.h>

//log
#include "../tdreamsock/dreamSockLog.h"

//game
#include "../game/game.h"


ShapeDynamicOgre::Shape(unsigned int index, Game* game, Client* client, Vector3D* position, Vector3D* velocity, Vector3D* rotation, Ogre::Root* root,
	  bool animated, bool collidable, float collisionRadius, int meshCode, bool ai)
:
	ShapeDynamic         (index,game,client,position,velocity,rotation,root,animated,collidable,
		collisionRadius,meshCode,ai)
{
	//we use this to name shape. as ogre is picky about same names. it also serves as a counter of sorts.
	Ogre::SceneManager* mSceneManager = root->createSceneManager(Ogre::ST_GENERIC);

	//create node to represent shape on server and pass in spawnPoint
	Vector3 spawnPoint = converToVector3(position);
	mSceneNode = mSceneManager->getRootSceneNode()->createChildSceneNode(spawnPoint);
}

ShapeDynamicOgre::~ShapeDynamicOgre()
{
	delete mSceneNode;
}

void ShapeDynamicOgre::scale(Vector3D scaleVector)
{
	getSceneNode()->scale(scaleVector.x, scaleVector.y, scaleVector.z);
}

void ShapeDynamicOgre::setRotation(Vector3D* vector3D)
{
	Vector3 vec;
	vec.x = vector3D->x;
	vec.y = vector3D->y;
	vec.z = vector3D->z;
	getSceneNode()->setDirection(vec,Ogre::Node::TS_WORLD);
}

Vector3D* ShapeDynamicOgre::getRotation()
{
	Vector3D* vector3D = new Vector3D();
	
	vector3D->x = getSceneNode()->_getDerivedOrientation().zAxis().x * -1;
	vector3D->y = 0.0;
	vector3D->z = getSceneNode()->_getDerivedOrientation().zAxis().z * -1;

	return vector3D;
}

void ShapeDynamicOgre::yaw(float amountToYaw, bool convertToDegree)
{
	if (convertToDegree)
	{
		getSceneNode()->yaw(Degree(amountToYaw));
		//1 = due west, -1 = due east, 0 = due south, -2 due east, 2 = due west
		Vector3 direction;
		direction.x = 1; //southwest
		direction.y = 0;
		direction.z = -1; 

		direction.normalise();
	}
}

void ShapeDynamicOgre::translate(Vector3D* translateVector, int perspective)
{
	if (perspective == 1)
	{
		getSceneNode()->translate(converToVector3(translateVector), Ogre::Node::TS_WORLD);
	}
	if (perspective == 2)
	{
		getSceneNode()->translate(converToVector3(translateVector), Ogre::Node::TS_LOCAL);
	}
}

void ShapeDynamicOgre::setPosition(Vector3D* position)
{
	getSceneNode()->setPosition(converToVector3(position));
}

void ShapeDynamicOgre::setPosition(float x, float y, float z)
{
	getSceneNode()->setPosition(x,y,z);
}

Vector3D* ShapeDynamicOgre::getPosition()
{
	Vector3D* position = new Vector3D();
	position->x = getSceneNode()->getPosition().x;
	position->y = getSceneNode()->getPosition().y;
	position->z = getSceneNode()->getPosition().z;
	return position;
}

Ogre::Vector3 ShapeDynamicOgre::converToVector3(Vector3D* vector3d)
{
	Ogre::Vector3 vec3;
	vec3.x = vector3d->x;
	vec3.y = vector3d->y;
	vec3.z = vector3d->z;
	return vec3;
}


